<?php
// 检查是否是ajax请求
function isAjax(){
	$ajax = false;
	if(!empty($_SERVER['HTTP_X_REQUESTED_WITH'])
		&& strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'){
	  $ajax = true;
	}
	return $ajax;
}

function isspage($total,$nownum,$psize,$page,$filename="",$parameter=""){
	$isspage_str="";
	$isspage_str1="";
	$totalpage=ceil($total/$psize);
	$isspage_str.="页次<strong>{$page}/{$totalpage}</strong> &nbsp;每页<strong>{$psize}</strong>条 &nbsp;总数<font class='rowcount'>{$total}</font>条 &nbsp; &nbsp;当前显示<strong>{$nownum}</strong>条";
	if ( $page>1 ){
		$isspage_str.='<a href="#" pageNo="1">首页</a>&nbsp;<a href="#" pageNo="'.($page-1).'">上一页</a>';
	}
	if ( $page<$totalpage ){
		$isspage_str.='&nbsp;<a href="#" pageNo="'.($page+1).'">下一页</a>&nbsp;<a href="#" pageNo="'.$totalpage.'">尾页</a>&nbsp;转到：';
	}

	if ($totalpage>1){
		$maxPageBtnCount = 5;
		$halfBtnCount = floor($maxPageBtnCount/2);
		$pageStart = $page - $halfBtnCount;
		$pageEnd = $page + $halfBtnCount;
		if($pageStart<1){
			$pageEnd += (1-$pageStart);
			$pageStart = 1;
		}
		if($pageEnd>$totalpage){
			$pageEnd = $totalpage-1;
			$pageStart = max($totalpage-$maxPageBtnCount+1, 1);
		}

		for ($i=$pageStart;$i<=$pageEnd;$i++){
			if ($page==$i){
				$isspage_str1.='<a value="#" pageNo="'.$i.'" selected="selected">第'.$i.'页</a>';
			}else{
				$isspage_str1.='<a value="#" pageNo="'.$i.'">第'.$i.'页</a>';
			}
		}
	}

	$str='<div class="makepage">'.$isspage_str.'</div><div class="jumppage">'.$isspage_str1.'</div>';
	return $str;
}

function fetchUrl($url, $post_data, $cookie="", &$location=""){
	$ch = curl_init();

	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_HEADER, 1);
	if($post_data){
		// post数据
		curl_setopt($ch, CURLOPT_POST, 1);
		// post的变量
		curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
	}
	if($cookie){
		curl_setopt($ch,CURLOPT_COOKIE,$cookie);
	}
//	print_r($url);
//	print_r($post_data);

	$output = curl_exec($ch);
//	var_dump($output);

	$headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
	$header = substr($output, 0, $headerSize);
	$body = substr($output, $headerSize);

	preg_match_all('/Location\s*:\s*(.*?)\n/i', $header, $results);
//		var_dump($results);
	if(count($results[1])){
		$location = $results[1][0];
	}

	curl_close($ch);
	return $body;
}

function fetchUrl1($url, &$cookie="", &$location=""){
	$ch = curl_init();

	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_HEADER, 1);
//	print_r($url);
//	print_r($post_data);

	$output = curl_exec($ch);
//	var_dump($url);
//	var_dump($output);
//	exit();

	$headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
	$header = substr($output, 0, $headerSize);
	$body = substr($output, $headerSize);

	preg_match_all('/Set-Cookie\s*:\s*(JSESSIONID=.*?);/i', $header, $results);
//		var_dump($results);
	if(count($results[1])) {
		$cookie = $results[1][0];
	}

	preg_match_all('/Location\s*:\s*(.*?)\n/i', $header, $results);
//		var_dump($results);
	if(count($results[1])){
		$location = $results[1][0];
	}

	curl_close($ch);
	return $body;
}

function xml2assoc($xml) {
	$tree = null;
	while($xml->read())
		switch ($xml->nodeType) {
			case XMLReader::END_ELEMENT: return $tree;
			case XMLReader::ELEMENT:
				$node = array('tag' => $xml->name, 'value' => $xml->isEmptyElement ? '' : xml2assoc($xml));
				if(is_array($node['value'])){
					$tree[$xml->name][] = $node['value'];
				}else{
					$tree[$xml->name] = $node['value'];
				}
				break;
			case XMLReader::TEXT:
			case XMLReader::CDATA:
				$tree .= $xml->value;
		}
	return $tree;
}

function xml2array($data) {
//	print_r($data);
	$xml = new XMLReader();
	$xml->XML($data);
	$array = xml2assoc($xml);
	$xml->close();
	return $array;
}